﻿using HANS_BackStage.Model.WebModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HANS_BackStage.Model.DbModel;
using HANS_BackStage.Impl;

namespace HANS_BackStage.Common
{
    public  class MenuHelper
    {

        /// <summary>
        /// 根据权限生成菜单
        /// </summary>
        public static List<MenuItem> BuildAdminMenu(List<Backstage_permission> allPermissions)
        {
            var menuPermission = allPermissions
                    .Where(p => p.Ismenu &&(p.Parentid == null || p.Parentid == ""))/*p. && p.Area == "Admin" &&*/
                    .ToList();

            return GetSubMenu(menuPermission, allPermissions);
        }

        private static List<MenuItem> GetSubMenu(List<Backstage_permission> permissions, List<Backstage_permission> allPermissions)
        {
            var result = new List<MenuItem>();
            foreach (var permission in permissions.Where(p=>p.Ismenu).OrderBy(p => p.Ordernum))
            {
                var menuTmp = new MenuItem()
                {
                    Url = permission.Url,
                    Controller = permission.Controller,
                    Action = permission.Action,
                    MenuItemName = permission.Permissionname,
                    ChildrensMenuItem = GetSubMenu(Role_PermiServices.GetPerChildrenList(permission.Id).Where(p=>allPermissions.Count(ap=>ap.Id==p.Id)>0).ToList() ,allPermissions),
                    Css=permission.Icon_url,
                    Area=permission.Area
                };
                result.Add(menuTmp);
            }
            return result;
        }
    }
}
